package tgc.edu.tgq.bootstrap.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import tgc.edu.tgq.bootstrap.custom.BaseService;
import tgc.edu.tgq.bootstrap.entity.SysRole;
import tgc.edu.tgq.bootstrap.entity.Teacher;
import tgc.edu.tgq.bootstrap.web.form.TeacherFrom;
@Service
public class TeacherService extends BaseService<Teacher, Integer>{
	@Autowired
	private SysRoleService sysRoleService;
	
	public synchronized Boolean save(TeacherFrom form) {
		try {
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			Integer id=form.getId();
			Teacher model=new Teacher();
			if(id!=null) {
				model=findById(id);
			}else {
				model.setPassword(encoder.encode("1234"));
				List<SysRole> role=new ArrayList<SysRole>();
				role.add(sysRoleService.findById(2));
				model.setRoles(role);
			}
			BeanUtils.copyProperties(form, model);
			save(model);
			return true;
		}catch (Exception e) {
			return false;
		}
	}

}
